package com.ruoyi.system.interAttr.attr;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.UUIDGenerator;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.model.dto.api.ApiGameListDto;
import com.ruoyi.system.outto.dto.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 起飞属性匹配
 * @author ruoyi
 * @data 2024/7/3
 * @apiNote
 */
@Service
@Slf4j
public class HEITAOAttr implements AdapterAttr {

    /**
     * 获取游戏列表
     *
     * @return
     */
    @Override
    public List<GamesRes> getGames(SalePartner salePartner) {
        String url = "https://back.txhyxds.com/webApi/gameList";
        long timestamp = System.currentTimeMillis();
        String token = getToken(salePartner);
        Map<String, String> header = new TreeMap<>();
        Map<String, String> action = new HashMap<>();
        header.put("token", token);
        header.put("timestamp", String.valueOf(timestamp));
        String cover = getCover1(action) + salePartner.getSaleKey() + token + timestamp;
        log.info("加密前请求参数={}", cover);
        String sign = DigestUtils.md5Hex(cover);
        header.put("sign", sign);
        String response = HttpUtil.doGet(url, action, header);
        String data = JSONObject.parseObject(response).getString("data");
        List<ApiGameListDto> responseList = JSONObject.parseArray(data, ApiGameListDto.class);
        List<GamesRes> gamesResList = new ArrayList<>();

        for (ApiGameListDto dto : responseList) {
            GamesRes gamesRes = new GamesRes();
            gamesRes.setId(String.valueOf(dto.getId()));
            gamesRes.setGameName(dto.getName());
            gamesResList.add(gamesRes);
        }
        return gamesResList;
    }

    /**
     * 获取游戏区服列表
     *
     * @return
     */
    @Override
    public List<InterServerRes> getServer(String gameId, SalePartner salePartner) {

        List<InterServerRes> resultList = new ArrayList<>();
        String url = "https://back.txhyxds.com/webApi/gameInfo/";
        long timestamp = System.currentTimeMillis();
        String token = getToken(salePartner);
        Map<String, String> header = new TreeMap<>();
        Map<String, String> action = new HashMap<>();
        action.put("gameId", String.valueOf(gameId));
        header.put("token", token);
        header.put("timestamp", String.valueOf(timestamp));
        String cover = getCover1(action) + salePartner.getSaleKey() + token + timestamp;
        log.info("加密前请求参数={}", cover);
        String sign = DigestUtils.md5Hex(cover);
        header.put("sign", sign);
        String response = HttpUtil.doGet(url + String.valueOf(gameId), action, header);
        System.out.println(response);
        JSONObject responseJson = JSONObject.parseObject(response).getJSONObject("data");
        // 手游
        if (Objects.equals(responseJson.getInteger("type"), 0)) {
            List<JSONObject> systems = JSONObject.parseArray(responseJson.getString("systems"), JSONObject.class);
            List<JSONObject> carriers = JSONObject.parseArray(responseJson.getString("carriers"), JSONObject.class);
            List<JSONObject> groups = JSONObject.parseArray(responseJson.getString("groups"), JSONObject.class);
            List<JSONObject> servers = JSONObject.parseArray(responseJson.getString("servers"), JSONObject.class);
            {

                for (JSONObject system : systems) {
                    InterServerRes sysm = new InterServerRes();
                    sysm.setUuid(UUIDGenerator.generateUuid6());
                    sysm.setId(system.getString("id"));
                    sysm.setName(system.getString("fieldName"));
                    List<InterServerRes> resList = new ArrayList<>();
                    for (JSONObject carrier : carriers) {
                        InterServerRes carr = new InterServerRes();
                        carr.setUuid(UUIDGenerator.generateUuid6());
                        carr.setId(carrier.getString("id"));
                        carr.setName(carrier.getString("fieldName"));
                        List<InterServerRes> resList2 = new ArrayList<>();
                        for (JSONObject group : groups) {
                            if(group.getString("remark").contains(carrier.getString("fieldName"))) {
                                InterServerRes gro = new InterServerRes();
                                gro.setUuid(UUIDGenerator.generateUuid6());
                                gro.setId(group.getString("id"));
                                gro.setName(group.getString("fieldName"));
                                List<InterServerRes> resList3 = new ArrayList<>();
                                for (JSONObject server : servers) {
                                    if(server.getString("parentId").equals(group.getString("id"))) {
                                        InterServerRes ser = new InterServerRes();
                                        ser.setUuid(UUIDGenerator.generateUuid6());
                                        ser.setId(server.getString("id"));
                                        ser.setName(server.getString("fieldName"));
                                        resList3.add(ser);
                                    }
                                }
                                gro.setInterServerRes(resList3);
                                resList2.add(gro);
                            }
                        }
                        carr.setInterServerRes(resList2);
                        resList.add(carr);
                    }
                    sysm.setInterServerRes(resList);
                    resultList.add(sysm);
                }
            }
        }else {
            List<JSONObject> groups = JSONObject.parseArray(responseJson.getString("groups"), JSONObject.class);
            List<JSONObject> servers = JSONObject.parseArray(responseJson.getString("servers"), JSONObject.class);
            {
                for (JSONObject group : groups) {
                    InterServerRes sysm = new InterServerRes();
                    sysm.setUuid(UUIDGenerator.generateUuid6());
                    sysm.setId(group.getString("id"));
                    sysm.setName(group.getString("fieldName"));
                    List<InterServerRes> resList = new ArrayList<>();
                    for (JSONObject server : servers) {
                        if(server.getString("parentId").equals(group.getString("id"))) {
                            InterServerRes ser = new InterServerRes();
                            ser.setUuid(UUIDGenerator.generateUuid6());
                            ser.setId(server.getString("id"));
                            ser.setName(server.getString("fieldName"));
                            resList.add(ser);
                        }
                    }
                    sysm.setInterServerRes(resList);
                    resultList.add(sysm);
                }
            }
        }
        return resultList;
    }

    /**
     * 获取游戏属性列表
     *
     * @return
     */
    @Override
    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        String url = "https://back.txhyxds.com/webApi/gameAttrTemp/";
        long timestamp = System.currentTimeMillis();
        Map<String, String> header = new TreeMap<>();
        Map<String, String> action = new HashMap<>();
        String token = getToken(salePartner);
        action.put("gameId", String.valueOf(gameId));
        header.put("token", token);
        header.put("timestamp", String.valueOf(timestamp));
        String cover = getCover1(action) + salePartner.getSaleKey() + token + timestamp;
        log.info("加密前请求参数={}", cover);
        String sign = DigestUtils.md5Hex(cover);
        header.put("sign", sign);
        log.info("sign=====" + sign);
        String response = HttpUtil.doGet(url + String.valueOf(gameId), action, header);

        List<JSONObject> data = JSONObject.parseArray(JSONObject.parseObject(response).getString("data"),JSONObject.class);
        List<InterAttrRes> resultList = new ArrayList<>();
        for (JSONObject datum : data) {

            if (StringUtils.equals("1",type)){
                String fieldType = datum.getString("fieldType");
                if (StringUtils.equals("radio",fieldType)){
                    InterAttrRes res = new InterAttrRes();
                    res.setUuid(UUIDGenerator.generateUuid6());
                    res.setId(datum.getString("aavName"));
                    res.setName(datum.getString("fieldName"));
                    res.setRequired(datum.getBoolean("must"));
                    List<InterAttrRes> list = new ArrayList<>();
                    List<JSONObject> array = JSONObject.parseArray(datum.getString("values"), JSONObject.class);
                    for (JSONObject jsonObject : array) {
                        InterAttrRes res1 = new InterAttrRes();
                        res1.setUuid(UUIDGenerator.generateUuid6());
                        res1.setId(jsonObject.getString("id"));
                        res1.setName(jsonObject.getString("fieldName"));
                        res1.setRequired(false);
                        list.add(res1);
                    }
                    res.setInterServerRes(list);
                    resultList.add(res);
                }
            }else if (StringUtils.equals("2",type)){
                String fieldType = datum.getString("fieldType");
                if (StringUtils.equals("checkbox",fieldType)){
                    InterAttrRes res = new InterAttrRes();
                    res.setUuid(UUIDGenerator.generateUuid6());
                    res.setId(datum.getString("aavName"));
                    res.setName(datum.getString("fieldName"));
                    res.setRequired(datum.getBoolean("must"));
                    List<InterAttrRes> list = new ArrayList<>();
                    List<JSONObject> array = JSONObject.parseArray(datum.getString("values"), JSONObject.class);
                    for (JSONObject jsonObject : array) {
                        InterAttrRes res1 = new InterAttrRes();
                        res1.setUuid(UUIDGenerator.generateUuid6());
                        res1.setId(jsonObject.getString("id"));
                        res1.setName(jsonObject.getString("fieldName"));
                        res1.setRequired(false);
                        list.add(res1);
                    }
                    res.setInterServerRes(list);
                    resultList.add(res);
                }else if (StringUtils.equals("boxradio",fieldType)){
                    InterAttrRes res = new InterAttrRes();
                    res.setUuid(UUIDGenerator.generateUuid6());
                    res.setId(datum.getString("aavName"));
                    res.setName(datum.getString("fieldName"));
                    res.setRequired(datum.getBoolean("must"));
                    List<InterAttrRes> list = new ArrayList<>();
                    List<JSONObject> array = JSONObject.parseArray(datum.getString("values"), JSONObject.class);
                    for (JSONObject jsonObject : array) {
                        InterAttrRes res1 = new InterAttrRes();
                        res1.setUuid(UUIDGenerator.generateUuid6());
                        res1.setId(jsonObject.getString("id"));
                        res1.setName(jsonObject.getString("fieldName"));
                        res1.setRequired(false);

                        List<InterAttrRes> list2 = new ArrayList<>();
                        List<JSONObject> array2 = JSONObject.parseArray(jsonObject.getString("child"), JSONObject.class);
                        for (JSONObject jsonObject2 : array2) {
                            InterAttrRes res2 = new InterAttrRes();
                            res2.setUuid(UUIDGenerator.generateUuid6());
                            res2.setId(jsonObject2.getString("id"));
                            res2.setName(jsonObject2.getString("fieldName"));
                            res2.setRequired(false);
                            list2.add(res2);
                        }
                        list.add(res1);
                    }
                    res.setInterServerRes(list);
                    resultList.add(res);
                }
            }else if (StringUtils.equals("3",type)){
                String fieldType = datum.getString("fieldType");
                if (StringUtils.equals("number",fieldType) || StringUtils.equals("input",fieldType)){
                    InterAttrRes res = new InterAttrRes();
                    res.setUuid(UUIDGenerator.generateUuid6());
                    res.setId(datum.getString("aavName"));
                    res.setName(datum.getString("fieldName"));
                    res.setRequired(datum.getBoolean("must"));
                    resultList.add(res);
                }
            }
        }

        return resultList;
    }

    //获取token
    public static String getToken(SalePartner salePartner) {
        String url = "https://back.txhyxds.com/webApi/api_token";
        Map<String, String> params = new TreeMap<>();
        long timestamp = System.currentTimeMillis();
        String cover = timestamp + salePartner.getTwoOther() + salePartner.getSaleKey();
        String sign = DigestUtils.md5Hex(cover);
        params.put("timestamp", String.valueOf(timestamp));
        params.put("sign", sign);
        Map<String, String> action1 = new TreeMap<>();
        action1.put("appId", salePartner.getTwoOther());
        String response = HttpUtil.doGet(url, action1, params);
        String data = JSONObject.parseObject(response).getString("data");
        String token = JSONObject.parseObject(data).getString("token");
        return token;
    }

    public static String getCover1(Map<String, String> params) {
        StringBuffer resut = new StringBuffer();
        for (String key : params.keySet()) {
            resut.append(key + "=" + params.get(key) + "&");
        }

        return resut.toString();
    }
}
